from myapp import db
from datetime import datetime

class OperationLog(db.Model):
    __tablename__ = 'sys_operation_log'
    id = db.Column(db.Integer, primary_key=True)
    operation_name = db.Column(db.String(100))
    method = db.Column(db.String(10))
    path = db.Column(db.String(200))
    params = db.Column(db.Text)
    operator = db.Column(db.String(50))
    operation_time = db.Column(db.Integer)
    response = db.Column(db.Text)
    exeception = db.Column(db.Text)
    result = db.Column(db.Integer)
    create_time = db.Column(db.DateTime, index=True, default=datetime.now)

    def __repr__(self):
        return f'<OperationLog {self.ID}>\n'
    
    def to_json(self):
        return {
            'id': self.id,
            'operationName': self.operation_name,
            'method': self.method,
            'path': self.path,
            'params': self.params,
            'operator': self.operator,
            'operationTime': self.operation_time,
            'response': self.response,
            'exception': self.exeception,
            'result': self.result,
            'createTime': self.create_time.strftime('%Y-%m-%d %H:%M:%S')
        }   